# _*_ coding utf-8 _*_
# FILENAME：orderPlanDetailList.py
# DESCRIPTION: 排单明细管理
# AUTH: jarvis zhang
# DATE: 2022/5/31 10:53 上午

from PyQt5 import QtGui
from PyQt5.QtWidgets import *
from PyQt5.Qt import *
from sqlalchemy import desc
import qtawesome as qta

from components.BaseDetailWindow import BaseDetailWindow
from models.baseModel import get_session, delete
from models.orderPlanModel import OrderPlanDetailsModel
from views.bom.uploadDialog import UploadDialog
from views.bom.versionDialog import VersionDialog


class OrderPlanDetailList(BaseDetailWindow):
    __versionId = 0
    __title = ''

    def __init__(self, versionId, title):
        super(OrderPlanDetailList, self).__init__()
        self.__init_ui()
        self.__versionId = versionId
        self.__title = title

    ''' UI '''
    def __init_ui(self):
        print('init ui')
        # 信号槽连接
        # self.btn_add.clicked.connect(self.add_click)
        # self.btn_close.clicked.connect(self.close)

    ''' Methods '''
    #  窗体show
    def show(self):
        super(OrderPlanDetailList, self).show()
        self.setWindowTitle("排单计划明细管理-" + self.__title)
        # 设置表头
        self.setTableViewHeaders()
        # 查询
        total = self.query_total()
        self.setTotalRecordCount(total)  # 总量
        self.recordQuery(0)  # page

        # 关闭窗体,释放资源
    def close(self):
        super(OrderPlanDetailList, self).close()

    def setTableViewHeaders(self):
        self.tableeHeaderLabels = ['编号', '货车编号', '物流车型', '供应商名称', '供应商地址', '零件编号', '零件名称', '零件数量', '箱数', '单包装容量', '包装类型', '目的地工厂', '交付道口', '路程距离', '装载率', '路程成本', '趟数', '预计抵达时间']
        self.dataModel.setHorizontalHeaderLabels(self.tableeHeaderLabels)
        # 设置列宽
        self.tableView.setColumnWidth(0, 50)
        self.tableView.setColumnWidth(1, 100)
        self.tableView.setColumnWidth(2, 200)
        self.tableView.setColumnWidth(3, 120)
        self.tableView.setColumnWidth(4, 100)
        self.tableView.setColumnWidth(5, 200)
        self.tableView.setColumnWidth(6, 200)
        self.tableView.setColumnWidth(7, 200)
        self.tableView.setColumnWidth(8, 200)
        self.tableView.setColumnWidth(9, 200)
        self.tableView.setColumnWidth(10, 200)
        self.tableView.setColumnWidth(11, 200)
        self.tableView.setColumnWidth(12, 200)
        self.tableView.setColumnWidth(13, 200)
        self.tableView.setColumnWidth(14, 200)
        self.tableView.setColumnWidth(15, 200)
        self.tableView.setColumnWidth(16, 200)
        self.tableView.setColumnWidth(17, 200)
        #  设置代理按钮,[btnEdit, btnUpload, btnCal, btnView, btnDel]
        # delegateBtnIsHiddes = [False, False, True, False, False]
        # self.tableView.setItemDelegateForColumn(4, CustomButtonDelegate(self, delegateBtnIsHiddes))

    # 记录查询，重写
    def recordQuery(self, limitIndex):
        data = self.page(limitIndex)
        self.dataModel.clear()
        if len(data) > 0:
            for i in range(len(data)):
                # 添加
                newItem = QStandardItem(str(data[i].id))
                newItem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
                self.dataModel.setItem(i, 0, newItem)

                newItem = QStandardItem(str(data[i].van_code))
                newItem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
                self.dataModel.setItem(i, 1, newItem)

                newItem = QStandardItem(str(data[i].van_size))
                newItem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
                self.dataModel.setItem(i, 2, newItem)

                newItem = QStandardItem(str(data[i].supplier_name))
                newItem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
                self.dataModel.setItem(i, 3, newItem)

                newItem = QStandardItem(str(data[i].supplier_address))
                newItem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
                self.dataModel.setItem(i, 4, newItem)

                newItem = QStandardItem(str(data[i].parts_code))
                newItem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
                self.dataModel.setItem(i, 5, newItem)

                newItem = QStandardItem(str(data[i].parts_name))
                newItem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
                self.dataModel.setItem(i, 6, newItem)

                newItem = QStandardItem(str(data[i].parts_quantity))
                newItem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
                self.dataModel.setItem(i, 7, newItem)

                newItem = QStandardItem(str(data[i].parts_box_quantity))
                newItem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
                self.dataModel.setItem(i, 8, newItem)

                newItem = QStandardItem(str(data[i].parts_snp))
                newItem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
                self.dataModel.setItem(i, 9, newItem)

                newItem = QStandardItem(str(data[i].parts_packaging_type))
                newItem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
                self.dataModel.setItem(i, 10, newItem)

                newItem = QStandardItem(str(data[i].factory))
                newItem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
                self.dataModel.setItem(i, 11, newItem)

                newItem = QStandardItem(str(data[i].unloading_crossing))
                newItem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
                self.dataModel.setItem(i, 12, newItem)

                newItem = QStandardItem(str(data[i].distance))
                newItem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
                self.dataModel.setItem(i, 13, newItem)

                newItem = QStandardItem(str(data[i].loading_percentage))
                newItem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
                self.dataModel.setItem(i, 14, newItem)

                newItem = QStandardItem(str(data[i].cost))
                newItem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
                self.dataModel.setItem(i, 15, newItem)

                newItem = QStandardItem(str(data[i].trip_number))
                newItem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
                self.dataModel.setItem(i, 16, newItem)

                newItem = QStandardItem(str(data[i].arrival_time))
                newItem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
                self.dataModel.setItem(i, 17, newItem)
        self.setTableViewHeaders()

    # 分页查询列表
    def page(self, limitIndex=0):
        # 获取dbsession
        session = get_session()
        # 拼接查询
        my_query = session.query(OrderPlanDetailsModel)
        my_query = my_query.filter(OrderPlanDetailsModel.is_del == 0)
        my_query = my_query.filter(OrderPlanDetailsModel.order_plan_id == self.__versionId)
        # 拼接排序条件
        my_query = my_query.order_by(desc(OrderPlanDetailsModel.update_date))
        # 分页
        my_query = my_query.offset(limitIndex).limit(self.PageRecordCount)
        # 执行查询
        model = my_query.all()
        session.close()
        return model

    # 查询数据总量
    def query_total(self):
        # 获取dbsession
        session = get_session()
        # 拼接查询
        my_query = session.query(OrderPlanDetailsModel)
        my_query = my_query.filter(OrderPlanDetailsModel.is_del == 0)
        my_query = my_query.filter(OrderPlanDetailsModel.order_plan_id == self.__versionId)
        # 执行查询
        count = my_query.count()
        session.close()
        return count

    # 新增
    def add_click(self):
        VersionDialog.get_dialog(self)

    # 编辑，继承父类
    def edit_click(self):
        super(OrderPlanDetailList, self).edit_click()
        button = self.sender()
        if button:
            cell = button.index
            row = cell[0]
            id = self.dataModel.item(row, 0).text()
            VersionDialog.get_dialog(self, int(id))

    #  上传，继承父类
    def upload_click(self):
        button = self.sender()
        if button:
            cell = button.index
            row = cell[0]
            id = self.dataModel.item(row, 0).text()
            UploadDialog.get_dialog(self, id)
            print(row)

    #  查看，继承父类
    def view_click(self):
        print('view')
        # button = self.sender()
        # if button:
        #     cell = button.index
        #     row = cell[0]
        #     id = self.dataModel.item(row, 0).text()
        #     dialog = BomDetailList(self, int(id))
        #     dialog.show()

    # 删除，继承父类
    def del_click(self):
        choice = QMessageBox.question(self, '', '是否删除当前数据?', QMessageBox.Yes | QMessageBox.No)  # 1
        if choice == QMessageBox.Yes:  # 2
            button = self.sender()
            if button:
                cell = button.index
                row = cell[0]
                id = self.dataModel.item(row, 0).text()
                model = OrderPlanDetailsModel()
                model.id = int(id)
                delete(model)
                self.refresh()
        elif choice == QMessageBox.No:  # 4
            pass
